package cn.sciento.farm.devicemanager.gateway.uplink.service.farm;

import cn.sciento.farm.devicemanager.common.constant.MqttConstant;
import cn.sciento.farm.devicemanager.common.util.NumberUtil;
import cn.sciento.farm.devicemanager.dao.entity.DevCollectionType;
import cn.sciento.farm.devicemanager.dao.entity.DevDevice;
import cn.sciento.farm.devicemanager.gateway.model.farm.CircuitData;
import cn.sciento.farm.devicemanager.gateway.model.farm.DeviceUplink;
import cn.sciento.farm.devicemanager.gateway.model.farm.NodeData;
import cn.sciento.farm.devicemanager.gateway.model.uplink.CollectionData;
import cn.sciento.farm.devicemanager.gateway.model.uplink.CollectionType;
import cn.sciento.farm.devicemanager.gateway.model.uplink.ControlData;
import cn.sciento.farm.devicemanager.gateway.model.uplink.SensorConfig;
import cn.sciento.farm.devicemanager.gateway.uplink.service.base.AbstractStringAdapter;
import cn.sciento.farm.devicemanager.gateway.uplink.service.sensor.MessageAllDisposeService;
import cn.sciento.farm.devicemanager.service.CollectionTypeService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


@Service(value = MqttConstant.PROTOCOL_PREFIX + MqttConstant.PROTOCOL_FARM)
public class FarmDataAdapter extends AbstractStringAdapter {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    CollectionTypeService collectionTypeService;

    @Autowired
    private MessageAllDisposeService messageAllDisposeService;


    @Override
    public void analyseStringData(String[] subTopic, String message) {
        if (subTopic.length > 4 || subTopic.length < 2) {
            return;
        }

        String concentrator = subTopic[2];

        List<CollectionData> deviceDataList = new ArrayList<>();
        DeviceUplink uplinkFrame = JSON.parseObject(message,DeviceUplink.class);
        if(System.currentTimeMillis() - uplinkFrame.getCollectionTime() > 1000*3600*2){
            return;
        }
        for(NodeData nodeData : uplinkFrame.getList()){

                DevCollectionType devCollectionType = collectionTypeService.getById(nodeData.getColType());
                if(devCollectionType == null){
                    logger.warn("没有相关的采集类型 {}",nodeData.getColType());
                    continue;
                }
                CollectionData collectionData = new CollectionData();
                collectionData.setCircuit(nodeData.getCircuit());
                collectionData.setNode(nodeData.getNode());
                collectionData.setEui(uplinkFrame.getEui());
                collectionData.setType(nodeData.getColType());
                collectionData.setName(devCollectionType.getName());
                collectionData.setData(NumberUtil.float2float(nodeData.getValue(), 2));
                deviceDataList.add(collectionData);

        }
        for (CollectionData deviceData : deviceDataList) {
            messageAllDisposeService.changeDeviceData(deviceData);

        }
    }
}
